﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zero.Core.Domain;
using Zero.DataLayer;
using Zero.POCO;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper.Support;
using Zero.POCO.Interfaces;
using Zero.POCO.Support;

namespace Zero.Presenter
{
    public class EditionCampagnePresenter:IEditCampagnePresenter
    {
        private IEditCampagneView _editCampagneView;
        private IRepositoryVague<Vague, int> _VagueRepository;
        private IRepository<Annonceur, Guid> _AnnonceurRepository;
        private IRepository<Filter, Guid> _FilterRepository;
        private IRepository<PlanTV, Guid> _planTVRepository;
        private IRepository<PlanRadio, Guid> _planRadioRepository;
        private IRepository<PlanPresse, Guid> _planPresseRepository; 

        public EditionCampagnePresenter(IEditCampagneView editCampagneView,IRepositoryVague<Vague,int> vagueRepository,IRepository<Annonceur, Guid> annonceurRepository,IRepository<Filter, Guid> filterRepository,IRepository<PlanTV,Guid> planTVRepository,IRepository<PlanRadio,Guid> planRadioRepository,IRepository<PlanPresse,Guid> planPresseRepository )
        {
            _editCampagneView = editCampagneView;
            _AnnonceurRepository = annonceurRepository;
            _VagueRepository = vagueRepository;
            _FilterRepository = filterRepository;
            _planTVRepository = planTVRepository;
            _planRadioRepository = planRadioRepository;
            _planPresseRepository = planPresseRepository;
        }
        public void Display()
        {
            _editCampagneView.Filters = _FilterRepository.FindAll().ToList();
            _editCampagneView.Annonceurs = _AnnonceurRepository.FindAll().ToList();
            _editCampagneView.Vague = _VagueRepository.FindAll().ToList();


            var planPresses = _planPresseRepository.FindAll().ToList();
            var planRadios = _planRadioRepository.FindAll().ToList();
            var planTvs = _planTVRepository.FindAll().ToList();


            var listePresse = new List<PlanView>();
            foreach (var planPress in planPresses)
            {
                var tempPlan = new PlanView()
                    {
                        Id = planPress.id,
                        nom = planPress.Name,
                        nombreInsertion = planPress.InsertionPresses.Count
                    };
                var supports=planPress.InsertionPresses.Select(p => p.SupportPresse).Distinct();
                foreach (var supportPress in supports)
                {
                    var tempSupport = new PlanSupportView()
                        {
                            Support = tempPlan.nom,
                            NombreInsertion = planPress.InsertionPresses.Select(n=>n.SupportPresse.id==supportPress.id).Count()
                        };
                    tempPlan.PlanSupportViews=new List<PlanSupportView>();
                    tempPlan.PlanSupportViews.Add(tempSupport);
                }
                listePresse.Add(tempPlan);
            }


            var listeTv = new List<PlanView>();
            foreach (var planTv in planTvs)
            {
                var tempPlan = new PlanView()
                {
                    Id = planTv.id,
                    nom = planTv.Name,
                    nombreInsertion = planTv.InsertionTvs.Count
                };
                var supports = planTv.InsertionTvs.Select(p => p.SupportTv).Distinct();
                foreach (var supportTv in supports)
                {
                    var tempSupport = new PlanSupportView()
                    {
                        Support = tempPlan.nom,
                        NombreInsertion = planTv.InsertionTvs.Select(n => n.SupportTv.id == supportTv.id).Count()
                    };
                    tempPlan.PlanSupportViews=new List<PlanSupportView>();
                    tempPlan.PlanSupportViews.Add(tempSupport);
                }
                listeTv.Add(tempPlan);
            }





            var listeRd = new List<PlanView>();
            foreach (var planRd in planRadios)
            {
                var tempPlan = new PlanView()
                {
                    Id = planRd.id,
                    nom = planRd.Name,
                    nombreInsertion = planRd.InsertionRadios.Count
                };
                var supports = planRd.InsertionRadios.Select(p => p.SupportRadio).Distinct();
                foreach (var supportRd in supports)
                {
                    var tempSupport = new PlanSupportView()
                    {
                        Support = tempPlan.nom,
                        NombreInsertion = planRd.InsertionRadios.Select(n => n.SupportRadio.id == supportRd.id).Count()
                    };
                    tempPlan.PlanSupportViews=new List<PlanSupportView>();
                    tempPlan.PlanSupportViews.Add(tempSupport);
                }
                listeRd.Add(tempPlan);
            }
            _editCampagneView.PlanPresses = listePresse;
            _editCampagneView.PlanRds = listeRd;
            _editCampagneView.PlanTvs = listeTv;


        }

        public void WriteChanges()
        {
            throw new NotImplementedException();
        }
    }

    internal interface IEditCampagnePresenter
    {
        void Display();
        void WriteChanges();
    }


    public interface IEditCampagneView
    {
        List<Vague> Vague { get; set; }
        List<Annonceur> Annonceurs { get; set; }
        List<Filter> Filters { get; set; }
        List<PlanView> PlanTvs { get; set; }
        List<PlanView> PlanRds { get; set; }
        List<PlanView> PlanPresses { get; set; } 
    }

    public class PlanView
    {
        public Guid Id { get; set; }
        public string nom { get; set; }
        public int nombreInsertion { get; set; }
        
        public List<PlanSupportView> PlanSupportViews { get; set; } 
    }

    public class PlanSupportView
    {
        
        
        public string Support { get; set; }
        public int NombreInsertion { get; set; }
    }





    public class OptimizationPlanPresenter:IOptimizerPlanPresenter
    {

        private IOptimizationPlanView _optimizationPlanView;
        private IReadOnlyRepository<SupportPresse, int> _repositoryPresse;
        private IReadOnlyRepository<SupportTV, int> _repositoryTV;
        private IReadOnlyRepository<SupportRadio, int> _repositoryRadio;
        private PlanTV _plantv;
        private PlanRadio _planRadio;
        private PlanPresse _planPresse;
        public OptimizationPlanPresenter(IOptimizationPlanView optimizationPlanView,IReadOnlyRepository<SupportPresse, int> repositoryPresse, IReadOnlyRepository<SupportTV, int> repositoryTV, IReadOnlyRepository<SupportRadio, int> repositoryRadio,PlanTV plantv,PlanRadio planRadio,PlanPresse planPresse)
        {
            _optimizationPlanView = optimizationPlanView;
            _planPresse = planPresse;
            _planRadio = planRadio;
            _plantv = plantv;
            _planPresse = planPresse;
            _repositoryPresse = repositoryPresse;
            _repositoryRadio = repositoryRadio;
            _repositoryTV = repositoryTV;
        }

        public void Display()
        {
            var supportViews =new List<SupportView>();
            var listeTv = _repositoryTV.FindAll();
            foreach (var supportTV in listeTv)
            {
                var viewTv = new SupportView
                {
                    Support = supportTV.Libelle,
                    SupportId = supportTV.id,
                    Typemedia = TypeMedia.TV
                };
                if (_plantv.ListeMapSupportTarifs != null)
                {
                    var grille = _plantv.ListeMapSupportTarifs.First(ed => ed.SupportId == supportTV.id);
                    if (grille != null)
                    {
                        viewTv.GrilleTarifaire = grille.TarifTv.nom;
                        viewTv.GrilleId = grille.TarifTv.id;
                    }
                }
                supportViews.Add(viewTv);
            }


            var liste = _repositoryRadio.FindAll();
            foreach (var support in liste)
            {
                var view = new SupportView
                {
                    Support = support.Libelle,
                    SupportId = support.id,
                    Typemedia = TypeMedia.RD
                };
                if (_planRadio.ListeMapSupportTarifs != null)
                {
                    var grille = _planRadio.ListeMapSupportTarifs.First(ed => ed.SupportId == support.id);
                    if (grille != null)
                    {
                        view.GrilleTarifaire = grille.TarifRd.nom;
                        view.GrilleId = grille.TarifRd.id;
                    }
                }
                supportViews.Add(view);
                
            }

            var listepr = _repositoryPresse.FindAll();
            foreach (var support in listepr)
            {
                var view = new SupportView
                {
                    Support = support.Libelle,
                    SupportId = support.id,
                    Typemedia = TypeMedia.PR
                };
                if (_planPresse.ListeMapSupportTarifs != null)
                {
                    var grille = _planPresse.ListeMapSupportTarifs.First(ed => ed.SupportId == support.id);
                    if (grille != null)
                    {
                        view.GrilleTarifaire = grille.TarifPr.nom;
                        view.GrilleId = grille.TarifPr.id;
                    }
                }
                supportViews.Add(view);
                
            }
            _optimizationPlanView._SupportViews = supportViews;
        }

        public void WriteChanges()
        {
            throw new NotImplementedException();
        }
    }

    public interface IOptimizationPlanView
    {
        OptimizationPlanView _OptimizationPlanView { get; set; }
        List<SupportView> _SupportViews { get; set; }
    }

    public class SupportView
    {
        public String Support { get; set; }
        public int SupportId { get; set; }
        public String GrilleTarifaire { get; set; }
        public Guid GrilleId { get; set; }
        public TypeMedia Typemedia { get; set; }


    }

    public class OptimizationPlanView
    {
        public bool IsPlanTv { get; set; }
        public bool IsPlanRd { get; set; }
        public bool IsPlanPr { get; set; }
        public List<SupportView> SupportViews { get; set; }
        public DateTime DebutCompagne { get; set; }
        public DateTime FinCompagne { get; set; }
        public int Couverture { get; set; }
        public int Repetition { get; set; }
        public int GRP { get; set; }
        public int Prix { get; set; }
        public int Moyenneour { get; set; }
        public int MoyenneSeminae { get; set; }
        public int CPT { get; set; }
        public int Couv3 { get; set; }
        public OptimizePlanTv OptimizePlanTv { get; set; }
        public OptimizePlanTv OptimizePlanRd { get; set; }
        public OptimizePlanTv OptimizePlanPr { get; set; }
    }

    public class OptimizePlanTv
    {
        int Couverture { get; set; }
        int Repetition { get; set; }
        int GRP { get; set; }
        int Prix { get; set; }
        int Moyenneour { get; set; }
        int MoyenneSeminae { get; set; }
        int CPT { get; set; }
        int Couv3 { get; set; }
    }
    public class OptimizePlanRd
    {
        int Couverture { get; set; }
        int Repetition { get; set; }
        int GRP { get; set; }
        int Prix { get; set; }
        int Moyenneour { get; set; }
        int MoyenneSeminae { get; set; }
        int CPT { get; set; }
        int Couv3 { get; set; }
    }
    public class OptimizePlanPr
    {
        int Couverture { get; set; }
        int Repetition { get; set; }
        int GRP { get; set; }
        int Prix { get; set; }
        int Moyenneour { get; set; }
        int MoyenneSeminae { get; set; }
        int CPT { get; set; }
        int Couv3 { get; set; }
    }


    public interface IOptimizerPlanPresenter
    {

        void Display();
        void WriteChanges();
    }
}
